var e = require("../../../utils/function.js"), t = (e.userImg, require("../../../api.js")), a = require("../../../utils/qqmap-wx.js"), i = getApp(), o = wx.getStorageSync("MAP_KEY") || "PNIBZ-QF5R2-XSGUL-CGVXI-5J4GZ-VNFNX", n = new a({
    key: o
});

Page({
    data: {
        dist: 0,
        formtime:" ",
        travel_type:'',
        license_plate_num:'',
        message: '司 机 已 接 单',
        istail:'',
        img: e.userImg,
        polyline: [ {
            points: [ {
                longitude: 108.93,
                latitude: 32.28
            }, {
                longitude: 108.94,
                latitude: 32.27
            } ],
            color: "#0299de",
            width: 9,
            dottedLine: !1,
            arrowLine: !0
        } ],
        controls: [ {
            id: 1,
            // iconPath: "../../../images/user/icon-22.png",
            position: {
                left: 10,
                top: 10,
                width: 50,
                height: 50
            },
            clickable: !0
        }, {
            id: 2,
            iconPath: "../../../images/user/icon-30.png",
            position: {
                top: 400,
               left: 400,
                width: 500,
                height: 500
            },
            clickable: !0
        } ],
        driver: wx.getStorageSync("serverDriver"),
        not_going_m: 0,
        not_going_s: 0,
        startAddress: null,
        myShow: !0,
        zuoyou: "icon-zuoyou",
        avatar: "../../../images/user/icon-07.png",
        buttonText: "登录",
        username: "未登录",
        orderGoing: !1,
        
        homeHandlerShow: !0,
        phone: "",
        order_no: "",
        is_service: 0,
        marks: [],
        make: [ {
            iconPath: "../../../images/user/my-location.png",
            id: 0,
            latitude: 34.29418,
            longitude: 108.94712,
            width: 89,
            height: 25
        }, {
            iconPath: "../../../images/user/driver-img.png",
            id: 2,
            latitude: 34.30518,
            longitude: 108.94712,
            width: 20,
            height: 20
        }, {
            iconPath: "../../../images/user/icon-07.png",
            id: 3,
            latitude: 23.099994,
            longitude: 113.32452,
            width: 50,
            height: 50
        } ],
        time: "",
        texttit:true,
        ordernum: 0,
        graded: 0,
        avatar_url: "",
        been_driving: 0,
        work_phone: 0,
        left_width: 680,
        distance: "",
        is_showInfo: !0,
        is_pay: !1,
        is_pay_info: ""
    },
    controltap: function(t) {
        var a = this;
        if (1 === t.controlId) wx.navigateTo({
            url: "/pages/map/my/my"
        }); else e.getLatLntHandler(function(e) {
            a.setData({
                startAddress: e.startAddress
            });
        });
    },
    goToOrder: function(e) {
        if (1 == e.is_send && 1 == e.initiate && 1 == e.is_pay) wx.redirectTo({
            url: "/pages/userOrder/orderDetail/orderDetail?order_no=" + e.order_no
        }); else if (0 == e.is_send && 0 == e.apply_delete) wx.redirectTo({
            url: "/pages/map/orderAndDriverGoing/orderAndDriverGoing?order_no=" + e.order_no
        }); else if (1 == e.apply_delete) wx.redirectTo({
            url: "../orderDetail/orderDetail?order_no=" + e.order_no
        }); else if (0 == e.is_service || 4 == e.is_service && 0 == e.apply_delete) ; else if (0 == e.apply_delete && 1 == e.is_service) wx.redirectTo({
            url: "../../map/orderGoing/orderGoing?order_no=" + e.order_no
        }); else if (0 == e.apply_delete && 2 == e.is_service) {
            if (1 == e.is_pay) return wx.redirectTo({
                url: "/pages/userOrder/orderDetail/orderDetail?order_no=" + e.order_no
            }), !1;
            wx.redirectTo({
                url: "/pages/pay/paidOrder/paidOrder?order_no=" + e.order_no
            });
        } else wx.redirectTo({
            url: "/pages/userOrder/orderDetail/orderDetail?order_no=" + e.order_no
        });
    },
    openMyPageHandler: function() {
        var e = this.data, t = (e.pageRight, e.myShow);
        t ? this.setData({
            pageRight: 620,
            left_width: 130,
            myShow: !t,
            controls: [ {
                id: 1,
                iconPath: "../../../images/user/icon-33.png",
                position: {
                    top: 144,
                    left: 10,
                    width: 41,
                    height: 41
                },
                clickable: !0
            } ]
        }) : (this.setData({
            pageRight: 0,
            left_width: 750,
            myShow: !t,
            controls: [ {
                id: 1,
                iconPath: "../../../images/user/icon-22.png",
                position: {
                    left: 10,
                    top: 10,
                    width: 50,
                    height: 50
                },
                clickable: !0
            }, {
                id: 2,
                iconPath: "../../../images/user/icon-30.png",
                position: {
                    top: 50,
                    right: 400,
                    width: 50,
                    height: 50
                },
                clickable: !0
            } ]
        }), this.changeIcon());
    },
    onLoad: function(e) {
        console.log(e);
    //    this.setData({
    //     istail: i.travel_type
    //    }),
        this.setData({
            order_no: e.order_no
        }), e.uid && i.shareCouponsMessage(e.uid), e.showdistance && this.setData({
            is_distance: !1,
            message: "司机已到达指定地点请乘客于三分钟内乘"
        });
    },
    going_order: function() {
        var e = this;
        wx.showModal({
            title: "提示",
            content: "预约时间未到，确认要提前出发吗？",
            success: function(a) {
                var o = a.confirm ? 2 : 3;
                i.request({
                    url: t.order_branch.confirm,
                    method: "post",
                    data: {
                        order_no: e.data.order_no,
                        setout: o
                    },
                    success: function(e) {}
                });
            }
        });
    },
    callDriverPhoneHandler: function() {
        e.callPhoneHandler(this.data.work_phone);
    },
    callServerPhoneHandler: function(t) {
        e.callPhoneHandler(t.currentTarget.dataset.phone);
    },
    deleteOrderHandler: function(e) {
        var a = this, o = e.detail.formId;
        i.request({
            url: t.order_branch.violations,
            method: "post",
            data: {
                formid: o,
                order_no: a.data.order_no
            },
            success: function(e) {
                1 == e.code ? wx.showModal({
                    title: "提示",
                    content: "下单超过" + e.data.time_limit + "分钟,需交付违约金" + e.data.violations + "元",
                    success: function(t) {
                        t.confirm && a.pay_penalty({
                            formid: o,
                            price: e.data.violations
                        });
                    }
                }) : 2 == e.code ? a.deleteOrder(o) : wx.showModal({
                    title: "提示",
                    content: e.msg
                });
            }
        });
    },
    deleteOrder: function(e) {
        var a = this;
        wx.showModal({
            title: "提示",
            content: "确认要取消订单",
            success: function(o) {
                o.confirm && i.request({
                    url: t.order.cancel_order,
                    method: "post",
                    data: {
                        formid: e,
                        order_no: a.data.order_no
                    },
                    success: function(e) {
                        i.gateway(a.data.order_no, "8"), 1 == e.code ? (wx.showToast({
                            title: "订单取消已成功"
                        }), clearInterval(this.timeTask), wx.reLaunch({
                            url: "../driverServer/driverServer"
                        })) : wx.showToast({
                            title: e.msg
                        });
                    }
                });
            }
        });
    },
    pay_penalty: function(e) {
        console.log(e);
        var a = this.data.order_no, o = e.formid, n = (e.price, this);
        console.log(n);
        i.request({
            url: t.order_branch.gold,
            method: "post",
            data: {
                formid: o,
                order_no: a ,
                cancel_damages: e.price
            },
            success: function(e) {
                wx.requestPayment({
                    timeStamp: e.data.timeStamp,
                    nonceStr: e.data.nonceStr,
                    package: e.data.package,
                    signType: e.data.signType,
                    paySign: e.data.paySign,
                    success: function(e) {
                        n.setData({
                            is_pay: !0,
                            is_pay_info: "支付成功"
                        }), clearInterval(n.timeTask), wx.navigateBack({
                            delta: 1
                        });
                    },
                    fail: function() {
                        n.setData({
                            is_pay: !0,
                            is_pay_info: "支付失败"
                        }), setTimeout(function() {
                            n.setData({
                                is_pay: !1,
                                is_pay_info: "支付失败"
                            });
                        }, 1500);
                    }
                });
            }
        });
    },
    timeTask: function() {
        var e = this.data.not_going_s, t = this.data.not_going_m;
        ++e > 59 && (e = 0, t++), this.setData({
            not_going_s: e,
            not_going_m: t
        });
    },
    orderGoingHandler: function() {
        wx.showToast({
            title: "目前还没有正在进行中的订单",
            icon: "none"
        }), this.setData({
            orderGoing: !1
        });
    },
    onReady: function() {},
    get_line_and_count_distance: function() {
        var e = this.data.startAddress, t = this.data.endAddress;
        e && t && this.getPolyline(e, t);
    },
    mark_icon: function() {
        var e = this, t = this.data.startAddress, a = this.data.endAddress, i = [];
        t && (i[0] = {
            id: 0,
            iconPath: "/images/user/icon-44.png",
            latitude: t.location.lat,
            longitude: t.location.lng,
            width: 40,
            height: 40
        }), a && (i[1] = {
            id: 1,
            iconPath: "/images/user/icon-43.png",
            latitude: a.location.lat,
            longitude: a.location.lng,
            width: 40,
            height: 40
        }), e.setData({
            make: i
        });
    },
    onShow: function() {
        var t = this
        this.getDriverInfo(), "" !== wx.getStorageSync("userMessage") && this.setData({
            avatar: wx.getStorageSync("userMessage").avatar,
            username: wx.getStorageSync("userMessage").username,
            buttonText: wx.getStorageSync("userMessage").buttonText,
            phone: wx.getStorageSync("phonep")
        });
        var t = this;
        
        2 == t.data.is_service ? (clearInterval(time), wx.removeStorageSync("is_delete"), 
        wx.redirectTo({
            url: "../orderGoing/orderGoing?order_no=" + t.data.order_no
        })) : 1 == t.data.is_service ? (wx.removeStorageSync("is_delete"), clearInterval(time), 
        wx.redirectTo({
            url: "../orderGoing/orderGoing?order_no=" + t.data.order_no
        })) : t.postionTask(), e.getLatLntHandler(function(e) {
            t.setData({
                startAddress: e.startAddress
            });
        }), this.changeIcon(), this.update_delete();
    },
    getDriverInfo: function() {
        var e = this, t = this;
        setTimeout(function() {
            var a = t.data.order_no, i = wx.getStorageSync("driver_now_position"), o = wx.getStorageSync("startAddress");
            i && i.order_no == a ? (t.getPolyline(JSON.parse(i.data.start_position_point), o.location), 
            t.setData({
                dist: i.data.dist,
                marks: [ {
                    iconPath: "../../../images/user/icon-50.png",
                    id: 1,
                    callout: {
                        content: "司机当前位置"
                    },
                    latitude: JSON.parse(i.data.start_position_point).latitude,
                    longitude: JSON.parse(i.data.start_position_point).longitude,
                    width: 40,
                    height: 40
                }, {
                    iconPath: "../../../images/driver/icon-44.png",
                    id: 2,
                    latitude: o.location.lat,
                    longitude: o.location.lng,
                    width: 40,
                    height: 40
                } ]
            }), 4 == i.data.is_service ? (e.setData({
                message: "司机已到达指定地点请乘客于三分钟内乘坐",
                texttit: false
            }),t.postionTask()): t.getDriverInfo()) : t.getDriverInfo();
        }, 5e3);
    },
    getPolyline: function(e, t) {
        var a = this, i = this.data.polyline, n = e.latitude + "," + e.longitude + "&to=" + t.lat + "," + t.lng ,p = '&policy=LEAST_DISTANCE';
        wx.request({
            url: "https://apis.map.qq.com/ws/direction/v1/driving/?from=" + n + "&key=" + o + p,
            header: {
                "content-type": "application/json"
            },
            success: function(e) {
                if (384 === e.data.status) ; else {
                    for (var t = e.data.result.routes[0].polyline, o = 2; o < t.length; o++) t[o] = t[o - 2] + t[o] / 1e6;
                    for (var n = [], r = 0; r < t.length; r += 2) n[n.length] = {
                        longitude: t[r + 1],
                        latitude: t[r]
                    };
                    i[0].points = n, a.setData({
                        polyline: i
                    });
                }
            },
            fail: function() {}
        });
    },
    update_delete: function() {
        var e = this;
        wx.getStorageSync("is_delete") || !1 ? this.setData({
            is_delete: !1
        }) : (this.setData({
            is_delete: !0
        }), setTimeout(function() {
            wx.setStorageSync("is_delete", !0), e.setData({
                is_delete: !1
            });
        }, 3e4));
    },
    get_distance: function(e, t, a) {
        n.calculateDistance({
            from: {
                latitude: e.location.lat,
                longitude: e.location.lng
            },
            to: [ {
                latitude: t.location.lat,
                longitude: t.location.lng
            } ],
            success: function(e) {
                a({
                    distance: e.result
                });
            },
            fail: function(e) {
                a(e);
            },
            complete: function(e) {}
        });
    },
    postionTask: function() {
        var e = this;
       
            i.request({
                url: t.order.waiting,
                method: "post",
                data: {
                    order_no: e.data.order_no
                },
                success: function(t) {
                  var p =  t.data.userinfo.real_name
               p =   p.substring(0, 1)
               console.log(p);
                    1 == t.code ? (e.goToOrder(t.data), wx.setStorageSync("startAddress", t.data.startAddress), 
                    wx.setStorageSync("endAddress", t.data.endAddress), e.setData({
                        order_no: t.data.order_no,
                        startAddress: t.data.lang,
                        endAddress: {
                            location: t.data.target_adds_location
                        },
                        make: t.data.start_location,
                        is_service: t.data.is_service,
                        drivername: p,
                        travel_type:t.data.travel_type,
                        ordernum: t.data.userinfo.ordernum,
                        graded: t.data.userinfo.graded,
                        avatar_url: t.data.userinfo.avatar_url,
                        been_driving: t.data.userinfo.been_driving,
                        work_phone: t.data.userinfo.work_phone,
                        signature: t.data.userinfo.signature,
                        distance: t.data.apart,
                        serverphone: t.data.serverphone,
                        price: t.data.total_price,
                        formtime:t.data.estimated_form_time,
                        license_plate_num:t.data.userinfo.license_plate_num,
                        is_distance: !0
                    }), 1 == t.data.is_out && e.data.is_showInfo && e.setData({
                        is_showInfo: !1
                    })) : wx.showToast({
                        title: t.msg,
                        icon: "none"
                    }), e.mark_icon();
                }
            });
        
      
    },
    loginHandler: function(t) {
        var a = this;
        "登录" === this.data.buttonText ? e.loginHandler(function(e) {
            a.setData({
                res: e
            });
            wx.showModal({
                title: "获取手机号码",
                content: "我们需要获取你的电话号码，一边与和你保持实时的联系，防止你的订单丢失",
                success: function(e) {
                    e.confirm && wx.reLaunch({
                        url: "../../user/getPhone/getPhone"
                    });
                },
                fail: function() {
                    wx.showToast({
                        title: "登录失败",
                        icon: "none"
                    });
                }
            });
        }) : this.setData({
            avatar: "../../../images/user/icon-07.png",
            username: "未登录",
            buttonText: "登录",
            phone: ""
        });
    },
    intoPageHandler: function(t) {
        e.intoPageHandler(t.currentTarget.dataset.skip);
    },
    changeIcon: function() {
        var e = this, t = this.data.controls;
        wx.getSystemInfo({
            success: function(a) {
                var i = a.windowWidth / 750 * 468, o = a.windowHeight - i;
                t[1] = {
                    id: 2,
                    // iconPath: "../../../images/user/icon-30.png",
                    position: {
                        top: o - 190,
                        left: 300 ,
                        width: 50,
                        height: 50
                    },
                    clickable: !0
                }, e.setData({
                    winPath: {
                        w: a.windowWidth,
                        h: a.windowHeight
                    },
                    map_height: o / 1.3 ,
                    controls: t
                });
            }
        });
    },
    onUnload: function() {
        var e = this.data.time;
        clearInterval(e);
    },
    onHide: function() {
        var e = this.data.time;
        clearInterval(e);
        
    },
    onShareAppMessage: function() {
        return {
            title: i.shareAppMessage().title,
            imageUrl: i.shareAppMessage().imageUrl,
            desc: i.shareAppMessage().desc,
            path: i.shareAppMessage().path
        };
    }
});